package com.wxz.gblw.cjg.controller;

import com.alibaba.fastjson.JSON;
import com.wxz.gblw.cjg.common.Result;
import com.wxz.gblw.cjg.common.model.RequestFilters;
import com.wxz.gblw.cjg.common.base.BaseWxzController;
import com.wxz.gblw.cjg.common.base.BaseWxzVoEntity;
import com.wxz.gblw.cjg.entity.KnowledgeType;
import com.wxz.gblw.cjg.mapper.KnowledgeTypeMapper;
import com.wxz.gblw.cjg.service.KnowledgeTypeService;
import com.wxz.gblw.cjg.vo.KnowledgeTypeVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* KnowledgeType
* @author xiangzhong.wang
* @date 2019-12-31 15:17:33
*/
@RestController
@RequestMapping("/knowledgeType")
public class KnowledgeTypeController extends BaseWxzController<KnowledgeTypeService, KnowledgeTypeMapper, KnowledgeType> {

    /**
     * 添加
     * @param model
     * @return 参数配置对象
     */
    @PostMapping
    //public boolean add(@Validated(BaseWxzVoEntity.Add.class) @RequestBody KnowledgeTypeVo model) {
    public Object add(@Validated(BaseWxzVoEntity.Add.class) @RequestBody KnowledgeTypeVo model) {
        Result result = new Result();
        result.setData(this.service.saveEntity(model));
        return result;
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @RequestMapping("/{id}")
    public Object get(@PathVariable Long id) {
        return this.service.getDetails(id);
    }

    /**
     * 编辑
     * @param model
     * @return
     */
    @PutMapping
    public boolean edit(@Validated(BaseWxzVoEntity.Update.class) @RequestBody KnowledgeTypeVo model) {
        return this.service.editEntity(model);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
//    public boolean delete(@PathVariable Long id) {
    public Object delete(@PathVariable Long id) {
        Result result = new Result();
        result.setData(this.service.deleteEntity(id));
        return result;
    }

    /**
     * 查询
     * @param filters 查询条件
     * @param currentPage 偏移量
     * @param pageSize 长度
     * @return
     */
    @GetMapping
    public Object search(@RequestParam long currentPage, @RequestParam long pageSize) throws Exception {
        //@RequestParam String filters,
        String filters="[]";
        List<RequestFilters> params = JSON.parseArray(filters, RequestFilters.class);

        Result result = new Result();
        result.setData(this.service.queryEntitiesByPage(params,currentPage,pageSize));
        return result;
    }

    /**
     * 知识类型树
     * @return
     */
    @GetMapping("/tree")
    public Object tree() {
        Result result = new Result();
        result.setData(this.service.tree());
        return result;
    }
}
